package cn.raven.org.service.impl;

import cn.raven.basic.query.BaseQuery;
import cn.raven.basic.service.impl.BaseServiceImpl;
import cn.raven.basic.utils.MD5Utils;
import cn.raven.basic.utils.PageList;
import cn.raven.basic.utils.StrUtils;
import cn.raven.org.domain.Employee;
import cn.raven.org.mapper.EmployeeMapper;
import cn.raven.org.service.IEmployeeService;
import cn.raven.system.domain.Role;
import cn.raven.system.mapper.RoleMapper;
import cn.raven.user.domain.Logininfo;
import cn.raven.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**

* @Description: 部门管理业务层
* @Author: king
* @Version: 1.0
* @Date:  2022/6/21 17:51
  */
@Service
//只读事务
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public void add(Employee employee) {
        // 1.初始化emplpyee，并保存，这是引用传递
        initEmployee(employee);
        employeeMapper.add(employee);
        // 2.初始化logininfo，并保存
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.add(logininfo);
        // 3.将logininfo的主键Id设置到employee的外键，并修改
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.update(employee);
        initAddIntermediateTable(employee);
    }
    /** 
    * @Title: initEmployee
    * @Description: 初始化emp数据
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/6 17:38  
    * @Parameters: [employee]
    * @Return void
      */
    private void initEmployee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String saltPassword = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setSalt(salt);
        employee.setPassword(saltPassword);
    }
    
    /** 
    * @Title: employee2Logininfo
    * @Description: 拷贝emp数据给logininfo
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/6 17:38  
    * @Parameters: [employee]
    * @Return cn.raven.user.domain.Logininfo
      */
    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfo.setId(employee.getLogininfoId());
        return logininfo;
    }
    
    /** 
    * @Title: update
    * @Description: 级联更新
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/6 18:07
    * @Parameters: [employee]
    * @Return void
      */
    @Override
    public void update(Employee employee) {
        initEmployee(employee);
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.update(logininfo);
        employeeMapper.update(employee);
        initDelIntermediateTable(employee.getId());
        initAddIntermediateTable(employee);
    }

    @Override
    public void del(Long id) {
        Employee emp = employeeMapper.findOne(id);
        initDelIntermediateTable(id);
        logininfoMapper.del(emp.getLogininfoId());
        employeeMapper.del(id);

    }

    @Override
    public PageList<Employee> queryData(BaseQuery baseQuery) {
        List<Employee> employees = employeeMapper.queryData(baseQuery);
        for (Employee employee: employees){
            employee.setRoleList(roleMapper.findRoleByEmployeeId(employee.getId()));
        }
        return super.queryData(baseQuery);
    }


    /**
     * @Title: initDelIntermediateTable
     * @Description: 删除menu和permission中间表数据
     * @Author: king
     * @Version: 1.0
     * @Date:  2022/7/13 11:07
     * @Parameters: [role]
     * @Return void
     */
    private void initDelIntermediateTable(Long id) {
        //先删除中间表数据
        employeeMapper.delEmployeeRole(id);
    }
    /**
     * @Title: initAddIntermediateTable
     * @Description: 添加menu和permission中间表数据
     * @Author: king
     * @Version: 1.0
     * @Date:  2022/7/13 11:07
     * @Parameters: [role]
     * @Return void
     */
    private void initAddIntermediateTable(Employee employee) {
        if (employee.getRoles() != null) {
            //在添加中间表数据
            employeeMapper.addEmployeeRole(employee.getId(),employee.getRoles());
        }
    }

    @Override
    public Employee findByLogininfoId(Long id) {
        return employeeMapper.findByLogininfoId(id);
    }

    @Override
    public List<Employee> findByShopId(Long shopId) {
        return employeeMapper.findByShopId(shopId);
    }
}
